package net.keebook.webapp.ws;

import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import net.keebook.webapp.dao.GroupDao;
import net.keebook.webapp.dao.PasswordDao;
import net.keebook.webapp.dao.SettingDao;
import net.keebook.webapp.entity.KeeBook;
import net.keebook.webapp.entity.PasswordItem;
import net.keebook.webapp.entity.User;
import net.keebook.webapp.util.AccessRight;

import org.apache.log4j.Logger;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


@Path("/{username}/deleteitem")
@Stateless
public class DeleteItemService extends WebService {
	@Inject
	private PasswordDao passwordDao;
	@Inject
	private SettingDao settingDao;
	@Inject
	private GroupDao groupDao;
	
	@POST
	@Consumes(MediaType.APPLICATION_JSON)
	@Produces(MediaType.APPLICATION_JSON)
	public String saveSettings(@PathParam("username") String username, String jsonString) {
		Logger.getLogger(this.getClass()).info("Incoming request");
		Logger.getLogger(this.getClass()).trace("Request string: " + jsonString);
		
		JSONObject input = getInput(jsonString);
		JSONObject result = new JSONObject();
		try {
			String password = input.getString("password");
			String keeBookId = input.getString("keeBook");
			String keeBookChecksum = input.getString("keeBookChecksum");
			JSONArray ids = input.getJSONArray("itemsIds");
			
			// Authentication
			User user = authenticate(username, password);
			if (user == null) {
				Logger.getLogger(this.getClass()).warn("Authentication failed for user " + username);
				result.put("authSuccess", false);
				result.put("success", false);
				return getResponse(result);
			}
			
			// Open Key Box
			KeeBook keeBook = getKeeBook(keeBookId, keeBookChecksum);
			if (keeBook == null || !getKeeBookDao().canAccess(user.getId(), keeBook.getId(), AccessRight.READ_WRITE)) {
				Logger.getLogger(this.getClass()).warn("Kee Book authentication failed for user " + username + " and kee book " + keeBookId);
				result.put("authSuccess", false);
				result.put("success", false);
				return getResponse(result);
			}
			
			Logger.getLogger(this.getClass()).info("User " + username + " authenticated successfully (user id: "+user.getExposableId()+" / kee book id: "+keeBook.getExposableId()+")");
			result.put("authSuccess", true);
			
			int numSuccessful = 0;
			for (int i=0; i<ids.length(); i++) {
				String id = ids.getString(i);
				PasswordItem item = getPasswordDao().get(id);
				if (item != null) {
					getPasswordDao().delete(item);
					numSuccessful++;
				}
			}
			
			result.put("success", true);
			result.put("count", numSuccessful);
		} catch (JSONException e) {
			return getResponse(ERROR_RESPONSE);
		}
		
		return getResponse(result);
	}

	public PasswordDao getPasswordDao() {
		return passwordDao;
	}

	public void setPasswordDao(PasswordDao passwordDao) {
		this.passwordDao = passwordDao;
	}

	public SettingDao getSettingDao() {
		return settingDao;
	}

	public void setSettingDao(SettingDao settingDao) {
		this.settingDao = settingDao;
	}

	public GroupDao getGroupDao() {
		return groupDao;
	}

	public void setGroupDao(GroupDao groupDao) {
		this.groupDao = groupDao;
	}
}
